//
//  QDQRCodeSettingViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/2/22.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit


fileprivate extension QDCodeSettingConfiguration.QDCodeSettingType {
    
    /// 描述
    var desc: String {
        get {
            switch self {
            case .metro: return "优先显示地铁乘车码"
            case .bus: return "优先显示公交乘车码"
            }
        }
    }
}


/// 乘车码设置界面，目前由于产品未给出流程，暂时不可用
final class QDQRCodeSettingViewController: NWTableViewController,QDLargeTitleTableHeaderScrollable {
    
    /// 大标题
    var largeTableHeaderView = QDLargeTitleTableHeaderView()
    /// 类型
    private let types: [QDCodeSettingConfiguration.QDCodeSettingType] = [.metro,.bus]
    /// 当前类型
    private var index = 0 {
        willSet {
            QDCodeSettingConfiguration.changedCodeSettingType(type: types[newValue]) //进行本地存储
        }
        didSet {
            tableView.selectRow(at: IndexPath(row: index, section: 0), animated: false, scrollPosition: .none)
        }
    }
    

    override func loadPropertysAtInitialization() {
        super.loadPropertysAtInitialization()
        
        headerRefreshEnable = false
        footerRefreshEnable = false
        tableView.rowHeight = 74.5
        tableView.allowsSelection = true
    }
    
    
    override func registerTableViewCellsAndReusableView() {
        tableView.register(QDQRTypeSelectedViewCell.self, forCellReuseIdentifier: "cell")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        largeTableHeaderView.title = "乘车码设置"
        largeTableHeaderView.frame = CGRect(origin: .zero, size: CGSize(width: ritl_width, height: QDLargeTitleTableHeaderViewNormalHeight))
        tableView.tableHeaderView = largeTableHeaderView
        
        //根据情况，进行选择
        index = (types as NSArray).index(of: QDCodeSettingConfiguration.codeSettingType())
    }
    
    
    // MARK: NavigationBar
    override var nav_mutableBarShadowHidden: Bool { return true }
    
    
    // MARK: UITableViewDataSource
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return types.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        //
        if let selectedCell = cell as? QDQRTypeSelectedViewCell {
            
            selectedCell.titleLabel.text = types[indexPath.row].desc
            selectedCell.setSelected(indexPath.row == index, animated: false)
            /// 最后一个不存在下划线
            selectedCell.bottomLineView.isHidden = (indexPath.row == types.count - 1)
        }
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //点击
        index = indexPath.row
    }
    
    
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        //避免重复
        return index == indexPath.row ? nil : indexPath
    }
    
    
    
    // MARK: UIScrollViewDelegate
    
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        tableViewDidScroll(contentOfset: scrollView.contentOffset){
            setNavigationBarShadowViewHidden(!$0)
        }
    }
    
}
